package com.ps.util;

import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class PageHelper implements Serializable {
	private Logger log = Logger.getLogger(this.getClass());
	
	public static PageHelper getIinstance(HttpServletRequest request,
			int totalRow) {
		int pageSizeNum = 10;
		PageHelper pageHelper = null;
		try {
			pageHelper = new PageHelper(request, totalRow, pageSizeNum);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return pageHelper;
	}

	public static PageHelper getIinstance(HttpServletRequest request,
			int totalRow, int pageSizeNum) {
		PageHelper pageHelper = null;
		try {
			pageHelper = new PageHelper(request, totalRow, pageSizeNum);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return pageHelper;
	}

	public PageHelper(HttpServletRequest request, int totalRow, int pageSizeNum)
			throws Exception {
		offset = "";
		pageNoStr = "";
		pageSizeStr = "";
		startRow = 0;
		ROWMIN = 0;
		ROWMAX = 0;
		this.totalRow = 0;
		totalPage = 0;
		pageNo = 0;
		pageSize = 10;
		try {
			pageNoStr = request.getParameter("pageNo");
			if(pageNoStr==null)
				pageNoStr="";
			pageSizeStr = StringUtils.isNotBlank(request.getParameter("pageSize")) ? request.getParameter("pageSize") : "";
			pageNo = 1;
			if (pageSizeNum == 0)
				pageSize = 10;
			else
				pageSize = pageSizeNum;
			if (!pageNoStr.equals(""))
				pageNo = Integer.parseInt(pageNoStr);
		
			if (!pageSizeStr.equals(""))
				pageSize = Integer.parseInt(pageSizeStr);
			ROWMIN = pageSize * (pageNo - 1) + 1;
			ROWMAX = pageSize * pageNo + 1;
			this.totalRow = totalRow;
			totalPage = (int) Math.ceil(((totalRow + pageSize) - 1) / pageSize);
			if (totalPage == 0)
				totalPage = 1;
			startRow = this.totalRow - pageSize * (pageNo - 1);
		} catch (Exception ex) {
			log.error(ex);
		}
	}

	public String getOffset() {
		return offset;
	}

	public int getPageNo() {
		return pageNo;
	}

	public String getPageNoStr() {
		return pageNoStr;
	}

	public int getPageSize() {
		return pageSize;
	}

	public String getPageSizeStr() {
		return pageSizeStr;
	}

	public int getROWMAX() {
		return ROWMAX;
	}

	public int getROWMIN() {
		return ROWMIN;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getTotalRow() {
		return totalRow;
	}

	public void setOffset(String offset) {
		this.offset = offset;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageNoStr(String pageNoStr) {
		this.pageNoStr = pageNoStr;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageSizeStr(String pageSizeStr) {
		this.pageSizeStr = pageSizeStr;
	}

	public void setROWMAX(int ROWMAX) {
		this.ROWMAX = ROWMAX;
	}

	public void setROWMIN(int ROWMIN) {
		this.ROWMIN = ROWMIN;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public String offset;

	public String pageNoStr;

	public String pageSizeStr;

	public int startRow;

	public int ROWMIN;

	public int ROWMAX;

	public int totalRow;

	public int totalPage;

	public int pageNo;

	public int pageSize;
}